package com.yc.bos.web.action;

import com.yc.bos.model.PageBean;
import com.yc.bos.model.Staff;
import com.yc.bos.service.IStaffService;
import com.yc.bos.web.action.base.BaseAction;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//@Controller
public class StaffAction extends BaseAction<Staff> {
    @Autowired
    private IStaffService staffService;

    @Override
    public String save() {
        System.out.println(getModel());
        staffService.save(getModel());

        return SUCCESS;
    }

    @Override
    public String update() {   //先查询sql,再设置一些参数,避免其他参数为null;也可以放在service里
     /*   System.out.println("表单提交:" + getModel());
        staffService.update(getModel());*/

        Staff staff = staffService.findById(getModel().getId());
        staff.setName(getModel().getName());
        staff.setTelephone(getModel().getTelephone());
        staff.setStation(getModel().getStation());
        staff.setHaspda(getModel().getHaspda());
        staff.setStandard(getModel().getStandard());
        System.out.println("数据库：" + staff);
        staffService.update(staff);


        return SUCCESS;
    }
//====================delete/ ============================
    private String ids;
    public void setIds(String ids) {
        this.ids = ids;
    }
    @Override
    public String delete() throws IOException {
       staffService.deleteBatch(ids);
        HttpServletResponse response= ServletActionContext.getResponse();
        response.getWriter().write("success");
        return NONE;
    }

    @Override
    public String list() {
        return null;
    }

    //==========================分页查询返回json数据=============================
    private  int page;
    private int rows;
    public void setPage(int page) {
        this.page = page;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }
    public  void pageQuery() throws IOException {
        PageBean<Staff> pd=new PageBean<Staff>();
        pd.setCurrentPage(page);
        pd.setPageSize(rows);
        DetachedCriteria dc= DetachedCriteria.forClass(Staff.class);
        pd.setDetachedCriteria(dc);
        staffService.pageQuery(pd);
        System.out.println(pd);
        JsonConfig config=new JsonConfig();
        config.setExcludes(new String[]{"currentPage","pageSize","detachedCriteria"});

        JSONObject jsonObject=JSONObject.fromObject(pd,config);
        HttpServletResponse response=ServletActionContext.getResponse();
        response.setHeader("context-type","text/json;charset=utf-8");
        response.getWriter().write(jsonObject.toString());
    }

    public  void  listJson() throws IOException {
        List<Staff> list=staffService.finaAllWithNoDelete();

         /* private String telephone;//手机
        private String haspda;//是否有台设备 0:无 1：有
        private String deltag = "0";//删除标志 0:正常在职 1：离职
        private String station;//所属单位
        private String standard;//收费标准*/
         responseJson(list,new String[]{"telephone","haspda","deltag","station","standard"});
    }
}
